package ving.mvc;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by liuweijie on 2019-12-07.
 */
public class HandlerServlet extends HttpServlet {

    private WebApplicationContext context;
    private MvcBeanFactory beanFactory;
    final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    private Configuration freemarkeConfig;

    @Override
    public void init() throws ServletException {
//        super.init();
        context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        beanFactory  = new MvcBeanFactory(context);
        Configuration freemarkeConfig = null;
        try{
            freemarkeConfig = context.getBean(Configuration.class);

        }catch (NoSuchBeanDefinitionException e){

        }

        if(freemarkeConfig ==null){
            freemarkeConfig = new Configuration(Configuration.VERSION_2_3_23);
            freemarkeConfig.setDefaultEncoding("UTF-8");
            freemarkeConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            try{
                freemarkeConfig.setDirectoryForTemplateLoading(new File(getServletContext().getRealPath("/ftl")));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
        this.freemarkeConfig =freemarkeConfig;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        doHandler(req,resp);
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        doHandler(req,resp);

    }

    // 处理静态文件
    private void doHandler(HttpServletRequest req, HttpServletResponse resp) {

        String uri = req.getServletPath();
        if(uri.equals("favicon.ico")){
            return ;
        }
        MvcBeanFactory.MvcBean mvcBean = beanFactory.getMvcBean(uri);

        if(mvcBean == null){
            throw new IllegalArgumentException(String.format("not found %s mapping",uri));

        }
        Object[] args = buildPrams(mvcBean,req,resp);

        try {
            Object result = mvcBean.run(args);
            processResult(result,resp);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 处理结果
    private void processResult(Object result, HttpServletResponse resp) throws IOException, TemplateException {
        if(result instanceof FreemarkeView){
            FreemarkeView fview = (FreemarkeView) result;
            Template template = freemarkeConfig.getTemplate(fview.getFtlPath());
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            resp.setContentType("text/html; charset = utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setStatus(200);
            template.process(fview.getModels(),resp.getWriter());

        }

        // JSTL VIEW

        // JSON VIEW
    }

    // 请求参数的类型转换

    private Object[] buildPrams(MvcBeanFactory.MvcBean mvcBean, HttpServletRequest req, HttpServletResponse resp) {

        // 拿出方法的参数名称
        Method method = mvcBean.getTargetMethod();
        String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);
        List<String> paramNames = Arrays.asList(parameterNames);

        // 拿出方法的参数类型
        Class<?>[] paramTypes = method.getParameterTypes();
        // a.b.c
        Object[] args = new Object[paramTypes.length];

        // 这里比较神奇就是拿出来的方法参数名称和方法类型是保存顺序的！！！！
        for (int i = 0; i < parameterNames.length; i++) {
            if(paramTypes[i].isAssignableFrom(HttpServletRequest.class)){
                args[i] = req;
            }else if(paramTypes[i].isAssignableFrom(HttpServletResponse.class)){
                args[i] =resp;

            }else {
                if(req.getParameter(paramNames.get(i))==null){
                    args[i] = null;
                }else {
                    args[i] = convert(req.getParameter(paramNames.get(i)),paramTypes[i]);
                }
            }


        }
        return args;
    }

    private <T>T convert(String val, Class<T> targetClass) {
        Object result = null;
        if(val ==null){
            return null;
        }else if(Integer.class.equals(targetClass)){
            result = Integer.parseInt(val.toString());

        }else if(Long.class.equals(targetClass)){
            result = Long.parseLong(val.toString());

        }else if(String.class.equals(targetClass)){
            result = val.toString();

        }else if(Date.class.equals(targetClass)){
            try {
                result =new SimpleDateFormat("").parse(val);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        else {
            System.err.println(String.format("not support param type %s",targetClass.getName()));
        }

        return (T) result;
    }

}
